package com.my.house.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.math.MathUtil;
import cn.hutool.core.util.NumberUtil;
import com.my.house.common.utils.JsonUtils;
import com.my.house.domain.Room;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import com.my.house.mapper.RoomMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户楼栋房间表 服务实现类
 * </p>
 *
 * @author yangll
 * @since 2025-02-08
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RoomService extends ServiceImpl<RoomMapper, Room> {


    /**
     * 根据楼栋id列表查询房间列表
     * @param buildingIds 楼栋id列表
     * @return 房间列表
     */
    public List<Room> findByBuildingIds(List<Long> buildingIds) {
        if (CollUtil.isEmpty(buildingIds)) {
            return CollUtil.newArrayList();
        }
        log.info("findByBuildingIds方法参数 buildingIds:{}", JsonUtils.easyToJsonString(buildingIds));
        return lambdaQuery().in(Room::getBuildingId, buildingIds).eq(Room::getDeleted, Boolean.FALSE).list();
    }

    /**
     * 根据楼栋id查询房间列表
     * @param buildingId 楼栋id
     * @return 楼栋的房间列表
     */
    public List<Room> findByBuildingId(Long userId, Long buildingId) {
        log.info("findByBuildingId方法参数 buildingId:{}", buildingId);
        return lambdaQuery().eq(Room::getUserId, userId)
                .eq(Objects.nonNull(buildingId) && buildingId > 0, Room::getBuildingId, buildingId)
                .eq(Room::getDeleted, Boolean.FALSE)
                .list();
    }

    /**
     * 添加房间
     * @param buildingId 楼栋id
     * @param name 房间名/号
     * @param waterNum  当前水表读数
     * @param electricNum 当前电表读数
     */
    public void addRoom(Long userId, Long buildingId, String name, Long waterNum, Long electricNum) {
        Room room = new Room();
        room.setUserId(userId);
        room.setBuildingId(buildingId);
        room.setName(name);
        room.setWaterNum(waterNum);
        room.setElectricNum(electricNum);
        room.setEmptyStatus(Boolean.TRUE);
        save(room);
    }

    /**
     * 编辑房间信息
     * @param id 房间id
     * @param buildingId 楼栋id
     * @param name  房间名号
     * @param waterNum 当前水表读数
     * @param electricNum 当前电表读数
     * @param userId 用户id
     */
    public void editRoomById(Long id, Long buildingId, String name, Long waterNum, Long electricNum, Long userId) {
        lambdaUpdate().eq(Room::getUserId, userId).eq(Room::getId, id)
                .set(Objects.nonNull(buildingId), Room::getBuildingId, buildingId)
                .set(Objects.nonNull(name), Room::getName, name)
                .set(Objects.nonNull(waterNum), Room::getWaterNum, waterNum)
                .set(Objects.nonNull(electricNum), Room::getElectricNum, electricNum)
                .update();
    }

    /**
     * 根据房间id删除房间
     * @param userId 用户id
     * @param id 房间id
     */
    public void deleteRoomById(Long userId, Long id) {
        lambdaUpdate().eq(Room::getUserId, userId).eq(Room::getId, id).set(Room::getDeleted, Boolean.TRUE).update();
    }

    /**
     * 根据楼栋id删除房间
     * @param userId 用户id
     * @param buildingId 楼栋id
     */
    public void deleteRoomByBuildingId(Long userId, Long buildingId) {
        lambdaUpdate().eq(Room::getUserId, userId).eq(Room::getBuildingId, buildingId).set(Room::getDeleted, Boolean.TRUE).update();
    }
}
